<?php
include_once '../inc/config.inc.php';
include_once '../inc/mysql.inc.php';

$link=connect();

// 分页处理
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$limit = 5; // 每页显示10条记录
$offset = ($page - 1) * $limit;

// 查询总记录数
$count_query = "SELECT COUNT(*) as total FROM member";
$count_result = execute($link, $count_query);
$total = mysqli_fetch_assoc($count_result)['total'];
$total_pages = ceil($total / $limit);

// 查询数据
$query = "SELECT * FROM member LIMIT $limit OFFSET $offset";
$result = execute($link, $query);
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理</title>
    <link rel="stylesheet" href="../css/table.css">
    <!-- 移除style标签 -->
</head>
<body>
    <h2 class="page-title">用户管理</h2>
    <div style="text-align:right;">
        <a href="add.php" class="add-btn">添加用户</a>
    </div>

    <table>
        <tr>
            <th>ID</th>
            <th>用户名</th>
            <th>密码</th>
            <th>性别</th>
            <th>电话</th>
            <th>地址</th>
            <th>邮箱</th>
            <th>操作</th>
        </tr>
        <?php while($data=mysqli_fetch_assoc($result)): 
            $data=escape($link,$data); ?>
        <tr>
            <td><?=$data['id']?></td>
            <td><?=$data['username']?></td>
            <td><?=$data['pw']?></td>
            <td><?=$data['sex']?></td>
            <td><?=$data['phonenum']?></td>
            <td><?=$data['address']?></td>
            <td><?=$data['email']?></td>
            <td>
                <div class="action-buttons">
                    <a href="edit.php?id=<?=$data['id']?>" class="edit-btn">编辑</a>
                    <a href="delete.php?id=<?=$data['id']?>" class="delete-btn" onclick="return confirm('确定要删除吗？')">删除</a>
                </div>
            </td>
        </tr>
        <?php endwhile; ?>
    </table>

    <!-- 分页导航 -->
    <div class="pagination">
        <?php if($page > 1): ?>
            <a href="?page=1">首页</a>
            <a href="?page=<?=$page-1?>">上一页</a>
        <?php endif; ?>

        <?php for($i = 1; $i <= $total_pages; $i++): ?>
            <a href="?page=<?=$i?>" <?=$i==$page?'class="active"':''?>><?=$i?></a>
        <?php endfor; ?>

        <?php if($page < $total_pages): ?>
            <a href="?page=<?=$page+1?>">下一页</a>
            <a href="?page=<?=$total_pages?>">末页</a>
        <?php endif; ?>
    </div>
</body>
</html>